home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / docs / linux-do / programm / lpg-0.4 / lpg-0 / LPG / examples / ipc / pipe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-01  |  1.0 KB  |  49 lines

  1. /*****************************************************************************
  2.  Excerpt from "Linux Programmer's Guide - Chapter 6"
  3.  (C)opyright 1994-1995, Scott Burkett
  4.  ***************************************************************************** 
  5.  MODULE: pipe.c
  6.  *****************************************************************************/
  7.  
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11.  
  12. int main(void)
  13. {
  14.     int     fd[2], nbytes;
  15.     pid_t    childpid;
  16.     char     string[] = "Hello, world!\n";
  17.     char    readbuffer[80];
  18.  
  19.     pipe(fd);
  20.     
  21.     if((childpid = fork()) == -1)
  22.     {
  23.         perror("fork");
  24.         exit(1);
  25.     }
  26.  
  27.     if(childpid == 0)
  28.     {
  29.         /* Child process closes up input side of pipe */
  30.         close(fd[0]);
  31.  
  32.         /* Send "string" through the output side of pipe */
  33.         write(fd[1], string, strlen(string));
  34.         exit(0);
  35.     }
  36.     else
  37.     {
  38.         /* Parent process closes up output side of pipe */
  39.         close(fd[1]);
  40.  
  41.         /* Read in a string from the pipe */
  42.         nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
  43.         printf("Received string: %s", readbuffer);
  44.     }
  45.     
  46.     return(0);
  47. }
  48.  
  49.